package servlet;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.CityDao;
import dao.DaoFactory;
import entity.City;

public class CityServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String pid=request.getParameter("pid");
		int parent=0;
		if(pid!=null&&!"".equals(pid)){
			parent=Integer.parseInt(pid);
		}
		CityDao dao= DaoFactory.getInstance(CityDao.class);
		response.setContentType("text/xml;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("<citys>");
		try {
			List<City> list = dao.findByParent(parent);
			for(City city:list){
				out.println("<city>");
				out.println("<id>"+city.getId()+"</id>");
				out.println("<name>"+city.getCityname()+"</name>");
				out.println("</city>");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		out.println("</citys>");
		out.flush();
		out.close();
	}
}
